package cn.hl.controller;

import cn.hl.domain.Employee;
import cn.hl.feign.EmployeeServer;
import cn.hl.sentinel.PayBlockHandler;
import cn.hl.sentinel.PayFallback;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private EmployeeServer employeeServer;

    @GetMapping("/getById/{id}")
    @SentinelResource(value = "pay-getById",
            blockHandler = "getById",
            blockHandlerClass = PayBlockHandler.class,
            fallback = "getEmployeeById",
            fallbackClass = PayFallback.class)
    public Employee getById(@PathVariable("id")Long id) {
//        int i = 1/0;
        return employeeServer.getById(id);
    }

    /**
     * 测试限流
     * @param id
     * @param e
     * @return
     */
    /*public Employee getById(@PathVariable("id")Long id, BlockException e) {
        return new Employee(-1L,"限流","服务不可用，请稍后重试！");
    }*/

    /**
     * 测试熔断
     * @param id
     * @param e
     * @return
     */
   /* public Employee getEmployeeById(@PathVariable("id")Long id, Throwable e) {
        return new Employee(-1L,"熔断","服务不可用，请稍后重试！");
    }*/

}
